package net.sevear.chaos.utils
{
	import com.adobe.serialization.json.JSON;
	
	import flash.utils.Dictionary;
	
	import net.sevear.chaos.download.DBRadioKeeper;
	import net.sevear.chaos.vo.TrackInfo;
	import net.sevear.chaos.vo.UserInfo;

	public class DataUtil
	{
		public function DataUtil()
		{
		}
		
		public static function generateNewTrack(newTrack:TrackInfo,origTrack:TrackInfo):void{
			if(newTrack==null) newTrack = new TrackInfo();
			newTrack.aid = origTrack.aid;
			newTrack.albumDBURL = origTrack.albumDBURL;
			newTrack.albumImgURL = origTrack.albumImgURL;
			newTrack.albumName = origTrack.albumName;
			newTrack.artistName = origTrack.artistName;
			newTrack.company = origTrack.company;
			newTrack.downURLs = new Dictionary();
			newTrack.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] = origTrack.downURLs[DBRadioKeeper.SOURCE_DB_RADIO];
			newTrack.downURLs[DBRadioKeeper.SOURCE_GOOGLE] = origTrack.downURLs[DBRadioKeeper.SOURCE_GOOGLE];
			newTrack.downURLs[DBRadioKeeper.SOURCE_SINA] = origTrack.downURLs[DBRadioKeeper.SOURCE_SINA];
			newTrack.downURLs[DBRadioKeeper.SOURCE_XIAMI] = origTrack.downURLs[DBRadioKeeper.SOURCE_XIAMI];
			newTrack.length = origTrack.length;
			newTrack.like = origTrack.like;
			newTrack.listenURL = origTrack.listenURL;
			newTrack.name = origTrack.name;
			newTrack.public_time = origTrack.public_time;
			newTrack.rating_avg = origTrack.rating_avg;
			newTrack.sid = origTrack.sid;
			newTrack.ssid = origTrack.ssid;
			newTrack.subtype = origTrack.subtype;
		}
		
		public static function parseRemoteTrack(str:String):TrackInfo{
			var song:Object = JSON.decode(str);
			var trackInfo:TrackInfo = new TrackInfo();
			trackInfo.name = song.name as String;
			trackInfo.albumName = song.aname as String;
			trackInfo.albumDBURL = song.adburl as String;
			trackInfo.artistName = song.artist;
			trackInfo.aid = parseInt(song.aid as String);
			trackInfo.albumImgURL = song.acoverurl as String;
			trackInfo.company = song.company as String;
			trackInfo.listenURL = song.listenurl as String;
			trackInfo.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] =  song.listenurl as String;
			trackInfo.downURLs[DBRadioKeeper.SOURCE_XIAMI] = song.xiamiurl as String;
			trackInfo.length = parseInt(song.length as String);
			trackInfo.like = false;
			trackInfo.rating_avg = parseFloat(song.rating_avg as String);
			trackInfo.sid = song.sid;
			trackInfo.ssid = song.ssid as String;
//			trackInfo.subtype = TrackInfo.INSTANT;
			return trackInfo;
		}
		
		public static function parseP2pUserInfo(remoteObj:Object,userInfo:UserInfo):void{
			userInfo.dbID = remoteObj.dbUID as int;
			userInfo.dbUID = remoteObj.dbAlias as String;
			userInfo.dbName = remoteObj.dbName as String;
			userInfo.dbURL = remoteObj.dbURL as String;
			userInfo.imgUrl = remoteObj.imgUrl as String;
			userInfo.isActivate = UserInfo.ACTIVATE_CHAOS;
			userInfo.lastActivitTime = remoteObj.lastActivitTime as Date;
			userInfo.location = remoteObj.location as String;
			userInfo.signature = remoteObj.signature as String;
		}
		
		public static function parseP2pTrackInfo(remoteObj:Object,trackInfo:TrackInfo):void{
			trackInfo.name = remoteObj.name as String;
			trackInfo.albumName = remoteObj.albumName as String;
			trackInfo.albumDBURL = remoteObj.albumDBURL as String;
			trackInfo.artistName = remoteObj.artistName;
			trackInfo.aid = parseInt(remoteObj.aid as String);
			trackInfo.albumImgURL = remoteObj.albumImgURL as String;
			trackInfo.company = remoteObj.company as String;
			trackInfo.listenURL = remoteObj.listenURL as String;
			trackInfo.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] =  remoteObj.downURLs[DBRadioKeeper.SOURCE_DB_RADIO];
			trackInfo.downURLs[DBRadioKeeper.SOURCE_XIAMI] = remoteObj.downURLs[DBRadioKeeper.SOURCE_XIAMI];
			trackInfo.downURLs[DBRadioKeeper.SOURCE_GOOGLE] = remoteObj.downURLs[DBRadioKeeper.SOURCE_GOOGLE];
			trackInfo.downURLs[DBRadioKeeper.SOURCE_SINA] = remoteObj.downURLs[DBRadioKeeper.SOURCE_SINA] 
			trackInfo.length = parseInt(remoteObj.length as String);
			trackInfo.rating_avg = parseFloat(remoteObj.rating_avg as String);
			trackInfo.sid = remoteObj.sid as int;
			trackInfo.ssid = remoteObj.ssid as String;
		}
		
		public static function parseChaosRecommendTrackInfo(remoteSong:Object,trackinfo:TrackInfo):void{
			trackinfo.name = remoteSong.name as String;
			trackinfo.albumName = remoteSong.aname as String;
			trackinfo.albumDBURL = remoteSong.adburl as String;
			trackinfo.artistName = remoteSong.artist;
			trackinfo.aid = parseInt(remoteSong.aid as String);
			trackinfo.albumImgURL = remoteSong.acoverurl as String;
			trackinfo.company = remoteSong.company as String;
			trackinfo.listenURL = remoteSong.listenurl as String;
			trackinfo.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] =  remoteSong.listenurl as String;
			trackinfo.downURLs[DBRadioKeeper.SOURCE_XIAMI] = remoteSong.xiamiurl as String;
			trackinfo.length = parseInt(remoteSong.length as String);
			trackinfo.like = false;
			trackinfo.rating_avg = parseFloat(remoteSong.rating_avg as String);
			trackinfo.sid = remoteSong.sid;
			trackinfo.ssid = remoteSong.ssid as String;
			trackinfo.subtype = TrackInfo.RECOMMEND;
		}
		
		public static function castStrToDate(dateString:String):Date {
			if ( dateString == null ) {
				return null;
			}
			
			var year:int = int(dateString.substr(0,4));
			var month:int = int(dateString.substr(5,2))-1;
			var day:int = int(dateString.substr(8,2));
			
			if ( year == 0 && month == 0 && day == 0 ) {
				return null;
			}
			
			if ( dateString.length == 10 ) {
				return new Date(year, month, day);
			}
			
			var hour:int = int(dateString.substr(11,2));
			var minute:int = int(dateString.substr(14,2));
			var second:int = int(dateString.substr(17,2));
			
			return new Date(year, month, day, hour, minute, second);
		}
	}
}